package de.szut.bomberman.game.logic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;

import de.szut.bomberman.gui.mainmenu.MainMenuPanel;
import de.szut.bomberman.main.Controller;

public class BombExplosion extends Thread {

	private JLabel playerOne;
	private JLabel playerTwo;
	private JLabel bomb;
	private ArrayList<JLabel> bombs;
	private ArrayList<JLabel> destroyableStones;
	private ArrayList<JLabel> destroyableStonesCopy;
	private JLayeredPane contentPane;

	public BombExplosion(JLabel playerOne, JLabel playerTwo, JLabel bomb,
			ArrayList<JLabel> stones, ArrayList<JLabel> destroyableStones,
			JLayeredPane contentPane, ArrayList<JLabel> bombs) {
		this.playerOne = playerOne;
		this.playerTwo = playerTwo;
		this.bomb = bomb;
		this.bombs = bombs;
		this.destroyableStones = destroyableStones;
		this.contentPane = contentPane;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void run() {
		this.destroyableStonesCopy = (ArrayList<JLabel>) destroyableStones
				.clone();
		try {

			sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		contentPane.remove(bomb);
		contentPane.repaint();
		bombs.remove(bomb);
		JLabel[] explosionLabels = new JLabel[6];
		try {
			for(int x = 0; x < 6; x++) {
				int xCor = 0;
				int yCor = 0;
				BufferedImage texture = ImageIO.read(new File("graphic-objects/Explosion.png"));
				JLabel label = new JLabel(new ImageIcon(texture));
				explosionLabels[x] = label;
				label.setSize(50, 50);
				if(x < 3) {
					xCor = (bomb.getX()-50)+x*50;
					if(xCor < 0) {
						xCor = 0;
					} else if(xCor > 550) {
						xCor = 550;
					}
				label.setLocation(xCor, bomb.getY());
				} else {
					yCor = (bomb.getY()-50)+(x-3)*50;
					if(yCor < 0) {
						yCor = 0;
					} else if(yCor > 550) {
						yCor = 550;
					}
					label.setLocation(bomb.getX(), yCor);
				}
				contentPane.add(label, new Integer(1));
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (JLabel destroyableStone : destroyableStonesCopy) {
			if (bomb.getX() + 50 == destroyableStone.getX()
					&& bomb.getY() == destroyableStone.getY()
					|| bomb.getX() - 50 == destroyableStone.getX()
					&& bomb.getY() == destroyableStone.getY()
					|| bomb.getX() == destroyableStone.getX()
					&& bomb.getY() - 50 == destroyableStone.getY()
					|| bomb.getX() == destroyableStone.getX()
					&& bomb.getY() + 50 == destroyableStone.getY()) {

				contentPane.remove(destroyableStone);
				contentPane.repaint();
				destroyableStones.remove(destroyableStone);
			}
		}
		if (bomb.getX() + 50 == playerOne.getX()
				&& bomb.getY() == playerOne.getY()
				|| bomb.getX() - 50 == playerOne.getX()
				&& bomb.getY() == playerOne.getY()
				|| bomb.getX() == playerOne.getX()
				&& bomb.getY() - 50 == playerOne.getY()
				|| bomb.getX() == playerOne.getX()
				&& bomb.getY() + 50 == playerOne.getY()
				|| bomb.getY() == playerOne.getY()
				&& bomb.getX() == playerOne.getX()
				&& !Controller.getInstance().isGameWon()) {

			Controller.getInstance().setGameWon(true);
			contentPane.remove(playerOne);
			JOptionPane.showMessageDialog(null, "Player 2 has won the Game!",
					"Win!", JOptionPane.INFORMATION_MESSAGE);
			Controller.getInstance().getGuiController().switchPanel(new MainMenuPanel());
			contentPane.repaint();
		}
		else if (bomb.getX() + 50 == playerTwo.getX()
				&& bomb.getY() == playerTwo.getY()
				|| bomb.getX() - 50 == playerTwo.getX()
				&& bomb.getY() == playerTwo.getY()
				|| bomb.getX() == playerTwo.getX()
				&& bomb.getY() - 50 == playerTwo.getY()
				|| bomb.getX() == playerTwo.getX()
				&& bomb.getY() + 50 == playerTwo.getY()
				|| bomb.getY() == playerTwo.getY()
				&& bomb.getX() == playerTwo.getX()
				&& !Controller.getInstance().isGameWon()) {

			Controller.getInstance().setGameWon(true);
			contentPane.remove(playerTwo);
			JOptionPane.showMessageDialog(null, "Player 1 has won the Game!",
					"Win!", JOptionPane.INFORMATION_MESSAGE);
			Controller.getInstance().getGuiController().switchPanel(new MainMenuPanel());
			contentPane.repaint();
		}
		try {
			sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(JLabel label : explosionLabels) {
			contentPane.remove(label);
		}
		contentPane.repaint(); 
	}
}
